﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Stock.Client.Utils
{
      public class HttpUtils
      {
            public static async Task<string> PostAsync(string json, string url, string para)
            {
                  string result = string.Empty;
                  using (HttpClient httpClient = new HttpClient(GetHttpClientHandler()))
                  {
                        httpClient.BaseAddress = new Uri(url);
                        HttpResponseMessage response = httpClient.PostAsync(url, GetHttpContent(json)).Result;
                        result = await response.Content.ReadAsStringAsync();
                  }
                  return result;
            }

            public static async Task<string> GetSendAsync(string url, string para="")
            {
                  string result = string.Empty;
                  using (HttpClient httpClient = new HttpClient(GetHttpClientHandler()))
                  {
                        httpClient.BaseAddress = new Uri(url);
                        HttpResponseMessage response = httpClient.GetAsync(url + para).Result;
                        result = await response.Content.ReadAsStringAsync();
                  }
                  return result;
        }

            private static HttpClientHandler GetHttpClientHandler()
            {
                  HttpClientHandler handler = new HttpClientHandler();
                  handler.AllowAutoRedirect = true;
                  handler.UseCookies = true;
                  //ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);//SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                  //SetCertificatePolicy();
                  return handler;
            }

            private static HttpContent GetHttpContent(string json)
            {
                  HttpContent httpContent = new StringContent(json);
                  httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;charset=UTF-8");
                  return httpContent;
            }


            /// <summary>
            /// Sets the cert policy.
            /// </summary>
            private static void SetCertificatePolicy()
            {
                  ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
            }
            /// <summary>
            /// Remotes the certificate validate.
            /// </summary>
            private static bool RemoteCertificateValidate(
               object sender, X509Certificate cert,
                X509Chain chain, SslPolicyErrors error)
            {
                  // trust any certificate!!!
                  //System.Console.WriteLine("Warning, trust any certificate");
                  return true;
            }


      }
}
